package com.cangsan.erhai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cangsan.erhai.entity.SysMenu;
import com.cangsan.erhai.entity.SysRole;
import com.cangsan.erhai.entity.SysUser;
import com.cangsan.erhai.mapper.SysMenuMapper;
import com.cangsan.erhai.mapper.SysRoleMapper;
import com.cangsan.erhai.mapper.SysUserMapper;
import com.cangsan.erhai.service.SysUserService;
import com.cangsan.erhai.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 76118
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2024-09-17 08:19:13
*/

@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public SysUser getByUserName(String userName) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = Wrappers.<SysUser>query().lambda();
        SysUser user = getOne(sysUserWrapper.eq(SysUser::getUserName, userName));


        return user ;
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
        StringBuffer authority = new StringBuffer();
        QueryWrapper<SysRole> getByRoleId = Wrappers.<SysRole>query().inSql("role_id", "select role_id from sys_user_role where user_id=" + userId);
        getByRoleId.eq("is_del",0);
        List<SysRole> sysRoles = sysRoleMapper.selectList(getByRoleId);
        String collect = sysRoles.stream().map(r -> "ROLE_" + r.getRoleKey()).collect(Collectors.joining(","));
        authority.append(collect);

        Set<String> menuCodeSet= new HashSet<>();

        for (SysRole sysRole : sysRoles) {
            QueryWrapper<SysMenu> get = Wrappers.<SysMenu>query().inSql("menu_id", "select menu_id from sys_role_menu where role_id=" + sysRole.getRoleId());
            get.eq("is_del",0);
            List<SysMenu> sysMenus = sysMenuMapper.selectList(get);
            for (SysMenu sysMenu : sysMenus) {
                String perms = sysMenu.getPerms();
                if(StringUtil.isNotEmpty(perms)){
                    menuCodeSet.add(perms);
                }
            }
        }
        authority.append(",");
        String menuPerms = menuCodeSet.stream().collect(Collectors.joining(","));
        authority.append(menuPerms);
    log.info(authority.toString());
        return authority.toString();
    }

    @Override
    public SysUser getByOpenId(String openId) {
        LambdaQueryWrapper<SysUser> eq = Wrappers.<SysUser>query().lambda().eq(SysUser::getOpenId, openId);
        return this.getOne(eq);
    }
}




